"""

"""

from kivymd.uix.card import MDCard
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.progressbar import MDProgressBar
from kivy.clock import Clock

class ProgressCard(MDCard):
    """"""
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation = 'vertical'
        self.padding = "20dp"
        self.spacing = "15dp"
        self.elevation = 3
        self.size_hint_y = None
        self.height = "200dp"
        
        self._build_ui()
    
    def _build_ui(self):
        """"""
        # 
        self.title_label = MDLabel(
            text=" ",
            theme_text_color="Primary",
            font_style="H6",
            size_hint_y=None,
            height="40dp"
        )
        self.add_widget(self.title_label)
        
        # 
        self.status_label = MDLabel(
            text="...",
            theme_text_color="Secondary",
            font_style="Body1",
            size_hint_y=None,
            height="30dp"
        )
        self.add_widget(self.status_label)
        
        # 
        self.progress_bar = MDProgressBar(
            value=0,
            size_hint_y=None,
            height="10dp"
        )
        self.add_widget(self.progress_bar)
        
        # 
        self.percent_label = MDLabel(
            text="0%",
            theme_text_color="Primary",
            font_style="H6",
            halign="center",
            size_hint_y=None,
            height="40dp"
        )
        self.add_widget(self.percent_label)
        
        # 
        self.detail_label = MDLabel(
            text="",
            theme_text_color="Hint",
            font_style="Caption",
            size_hint_y=None,
            height="40dp"
        )
        self.add_widget(self.detail_label)
    
    def update_progress(self, value, status="", detail=""):
        """"""
        def update_ui(dt):
            # 
            self.progress_bar.value = value
            
            # 
            self.percent_label.text = f"{value:.1f}%"
            
            # 
            if status:
                self.status_label.text = status
            
            # 
            if detail:
                self.detail_label.text = detail
            
            # 
            if value >= 100:
                self.progress_bar.color = [0, 1, 0, 1]  # 
                self.percent_label.theme_text_color = "Custom"
                self.percent_label.text_color = [0, 1, 0, 1]
            elif value >= 50:
                self.progress_bar.color = [1, 0.5, 0, 1]  # 
            else:
                self.progress_bar.color = [0, 0.5, 1, 1]  # 
        
        Clock.schedule_once(update_ui)
    
    def set_error(self, error_message):
        """"""
        def set_error_ui(dt):
            self.status_label.text = " "
            self.detail_label.text = error_message
            self.progress_bar.color = [1, 0, 0, 1]  # 
            self.percent_label.theme_text_color = "Custom"
            self.percent_label.text_color = [1, 0, 0, 1]
        
        Clock.schedule_once(set_error_ui)
    
    def reset(self):
        """"""
        self.update_progress(0, "...", "")
        self.progress_bar.color = [0, 0.5, 1, 1]  # 
        self.percent_label.theme_text_color = "Primary"